from sqlalchemy.orm import sessionmaker
from models import Teacher
from config import Config
from sqlalchemy import create_engine


engine = create_engine(Config.SQLALCHEMY_DATABASE_URI)
Session = sessionmaker(bind=engine)


def add_teacher(tno, tname, dno, salary, tmobile):
    session = Session()
    teacher = Teacher(Tno=tno, Tname=tname, Dno=dno, Salary=salary, Tmobile=tmobile)
    session.add(teacher)
    session.commit()
    session.close()


def delete_teacher_by_tno(tno):
    session = Session()
    teacher = session.query(Teacher).filter_by(Tno=tno).first()
    if teacher:
        session.delete(teacher)
        session.commit()
    session.close()


def query_teacher_by_name(name):
    session = Session()
    teachers = session.query(Teacher).filter(Teacher.Tname.like('%' + name + '%')).all()
    session.close()
    return teachers